from scapy.all import *
from scapy.fields import *
from scapy.packet import Packet

# 自定义 conga_h 协议头
class conga_h(Packet):
    name = "conga_h"
    fields_desc = [
        IPField("srcAddr", "0.0.0.0"),    # IPv4 地址（字符串格式）
        IPField("dstAddr", "0.0.0.0"),
        ShortField("hash", 0),            # 16-bit 字段
        ByteField("direction", 0),        # 8-bit 字段
        ByteField("LBTag", 0),
        IntField("CE", 0)                 # 32-bit 字段
    ]

# 绑定协议到以太网类型（假设自定义类型为 0x1234，需与实际环境一致）
bind_layers(Ether, conga_h, type=0x0810)

packet = Ether(type=0x0810) / conga_h(
    srcAddr="10.0.1.11",        # 源IP地址（字符串格式）
    dstAddr="10.0.2.11",   # 目标IP地址（字符串格式）
    hash=0xabcd,               # 16-bit哈希值
    direction=0,               # 方向标识
    LBTag=[i for i in range(4)],                # 负载均衡标签
    CE=0x00000000              # 32-bit CE值
)

# 发送报文（二层发送，无需等待回复
# while True:
#     time.sleep(1)
#     sendp(packet, iface="eth0", verbose=True)
#     break

sendp(packet, iface="eth0", verbose=True)

# packet_tag_0 = Ether(type=0x0810) / conga_h(
#     srcAddr="10.0.1.11",        # 源IP地址（字符串格式）
#     dstAddr="10.0.2.11",   # 目标IP地址（字符串格式）
#     hash=0xabcd,               # 16-bit哈希值
#     direction=0,               # 方向标识
#     LBTag=0,                # 负载均衡标签
#     CE=0x00000000              # 32-bit CE值
# )
# # sendp(packet_tag_0,iface="eth0",verbose=True)
